import {NativeModules} from 'react-native';
let localCookie = "";
module.exports = async function requestAPI(
  url,
  data = {},
  method = 'GET',
  cookie = '',
) {
  let me = 1;
  if (method === 'POST' || method === 'post') {
    me = 2;
  }
  if(localCookie === ""){
    await NativeModules.ToolModule.checkQQ().then((r) => {
      if(r.cookie){
        localCookie = r.cookie;
      }
    });
  }
  cookie = localCookie;
  // cookie = 'pgv_pvid=3972953017; RK=lfa1Mv8jN8; ptcz=c22ee9c4b92fb555d5d6bf99be866e9daa0cf8b00dcdc9faa2d3b5079e901c65; eas_sid=mznpWOrEvB2Nease25OPpCOWTW; pgv_pvi=754907136; pac_uid=1_751799928; ts_uid=8066390000; nutty_uuid=c456111a-9b9c-4252-bf1f-2b655f29d7ba; iip=0; tvfe_boss_uuid=11f097a9ec61ff61; _ga=GA1.2.1353443097.1622102015; LW_uid=B1T6m375l7R7F0F3X9A2u0L9b5; LW_sid=P1j6k3o5K7s790J4v1r935H6a7; uin_cookie=o1192673640; fqm_pvqid=07ccb50f-0558-4c60-82a0-1ca91571eafd; tmeLoginType=2; euin=7i457iEqNK-F; o_cookie=1192673640; ts_refer=www.baidu.com/link; ptui_loginuin=751799928; pgv_info=ssid=s2665577290; lolqqcomrouteLine=tftguide_tftguide; fqm_sessionid=293fa8d5-a506-4f61-822f-0f9bce9663e4; _qpsvr_localtk=0.33713143410271496; login_type=1; psrf_qqaccess_token=56FBA9D26555E41FF2F95D0DE64DCD62; wxunionid=; qm_keyst=Q_H_L_5mXNFGO7DzTXthl8y4i-cBK7PyLwU9krgUr7zkWuyEPckRfkgl8S_qg; wxopenid=; psrf_qqunionid=4A8004E767B34D8FF2A54718622C8EAB; psrf_qqopenid=11A334E3B5D88B0BFCFA63785264DB24; psrf_qqrefresh_token=429A05CE20EEB53001CB4868321AA487; qm_keyst=Q_H_L_5mXNFGO7DzTXthl8y4i-cBK7PyLwU9krgUr7zkWuyEPckRfkgl8S_qg; psrf_musickey_createtime=1649233449; uin=751799928; qqmusic_key=Q_H_L_5mXNFGO7DzTXthl8y4i-cBK7PyLwU9krgUr7zkWuyEPckRfkgl8S_qg; psrf_access_token_expiresAt=1657009449; wxrefresh_token=; ts_last=y.qq.com/n/ryqq/albumDetail/004e8xVp0LRSM8';
  return NativeModules.ToolModule.sendQQ(me, url, data, cookie)
    .then(r => {
      //console.log(r);
      if(url.startsWith('https://y.qq.com/n/yqq/album/')){
        return r.body
      }
      if (r.body) {
        if (r.body.startsWith('MusicJsonCallback')) {
          r.body = r.body.replace('MusicJsonCallback', '');
        } else if (r.body.startsWith('jsonCallback')) {
          r.body = r.body.replace('jsonCallback', '');
        }
        if(r.body[0] === '('){
          r.body = r.body.substring(1,r.body.length);
          if(r.body[r.body.length-1] && r.body[r.body.length-1] === ')' )
            r.body = r.body.substring(0,r.body.length-1);
        }
        return JSON.parse(r.body);
      }
    })
    .catch(r => {
      //console.log(r);
      return r;
    });
  // return  axios({
  //     url:url,
  //     data:data,
  //     headers:{
  //         "referer":"https://u.y.qq.com"
  //     }
  // }).then((r) => {
  //     //console.log(r)
  //     return r
  // })
};
